/**
 * Author: Jordi Vilaplana Mayoral <jvilaplana@alumnes.udl.cat>
 * Date: 05/04/2011
 * 
 * LocationManager
 * Copyright (C) 2011 Jordi Vilaplana Mayoral
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * @(#)
 * Description: This program allows users to book rooms and schedule activities
 */
package locmanager.servlets;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import locmanager.beans.ActivityBean;
import locmanager.beans.EquipmentBean;
import locmanager.beans.LocationBean;
import locmanager.beans.RoomTypeBean;
import locmanager.persistence.DerbyPersistence;

/**
 * New Activity Controller.
 * 
 * @author Jordi Vilaplana Mayoral <jvilaplana@alumnes.udl.cat>
 * @version 1.0
 */
public class NewActivityServlet extends HttpServlet {

  /**
   * Shows the new activity page.
   * 
   * @param request
   * @param response
   * @throws ServletException
   * @throws IOException 
   */
  private void showNewActivityMenu(HttpServletRequest request, HttpServletResponse response)
          throws ServletException, IOException {

    DerbyPersistence db = new DerbyPersistence();
    List<LocationBean> locationList = db.getLocations();

    List<EquipmentBean> equipmentList = db.getEquipments();
    List<RoomTypeBean> roomTypeList = db.getRoomTypes();

    request.setAttribute("equipmentList", equipmentList);
    request.setAttribute("locationList", locationList);
    request.setAttribute("roomTypeList", roomTypeList);
    request.setAttribute("m", "newActivity");

    getServletConfig().getServletContext().
            getRequestDispatcher("/index.jsp").forward(request, response);

  }

  /**
   * Handles all the requests to this controller
   * 
   * @param request
   * @param response
   * @throws ServletException
   * @throws IOException 
   */
  protected void processRequest(HttpServletRequest request, HttpServletResponse response)
          throws ServletException, IOException {

    if (request.getParameter("locationId") != null) {

      ActivityBean activity = new ActivityBean();

      DerbyPersistence db = new DerbyPersistence();
      HttpSession session = request.getSession(true);

      SimpleDateFormat formater = new SimpleDateFormat("yyyy-MM-dd");
      Date startDate = null;
      String[] selectedEquipments;
      List<EquipmentBean> locationEquipments = new ArrayList<EquipmentBean>();

      activity.setLocationId(Integer.parseInt(request.getParameter("locationId")));
      activity.setPublicName((String) request.getParameter("publicName"));
      activity.setMaxStudents(Integer.parseInt((String) request.getParameter("maxStudents")));
      activity.setDuration(Integer.parseInt((String) request.getParameter("duration")));
      activity.setManagerId((Integer) session.getAttribute("userId"));

      try {
        activity.setStartDate(formater.parse((String) request.getParameter("startDate")));
      } catch (Exception ex) {
      }




      selectedEquipments = request.getParameterValues("locationEquipments");
      if (selectedEquipments != null) {
        for (String equipment : selectedEquipments) {
          locationEquipments.add(db.getEquipmentById(Integer.parseInt(equipment)));
        }
      }
      activity.setEquipments(locationEquipments);

      db.insertActivity(activity);

      request.setAttribute("success", true);

    }

    showNewActivityMenu(request, response);

  }

  /** 
   * Handles the HTTP <code>GET</code> method.
   * @param request servlet request
   * @param response servlet response
   * @throws ServletException if a servlet-specific error occurs
   * @throws IOException if an I/O error occurs
   */
  @Override
  protected void doGet(HttpServletRequest request, HttpServletResponse response)
          throws ServletException, IOException {
    processRequest(request, response);
  }

  /** 
   * Handles the HTTP <code>POST</code> method.
   * @param request servlet request
   * @param response servlet response
   * @throws ServletException if a servlet-specific error occurs
   * @throws IOException if an I/O error occurs
   */
  @Override
  protected void doPost(HttpServletRequest request, HttpServletResponse response)
          throws ServletException, IOException {
    processRequest(request, response);
  }

}
